import { Global } from '@nestjs/common';
import { Comment } from '../../comment/entities/comment.entity';
import { Like } from '../../like/entities/like.entity';
import { User } from '../..//user/entities/user.entity';
import { UnsignedInt, utcToBeijing } from '../../utils';
import {
  Column,
  CreateDateColumn,
  Entity,
  ManyToMany,
  ManyToOne,
  OneToMany,
  PrimaryGeneratedColumn,
  UpdateDateColumn,
} from 'typeorm';
@Global()
@Entity({ name: 'daily' })
export class Daily {
  @PrimaryGeneratedColumn()
  id: number;

  @CreateDateColumn({
    type: 'timestamp',
    transformer: {
      to(value: Date): number {
        return value ? value.getTime() : null;
      },
      from(value: number) {
        // 将UTC时间转换为北京时间
        return utcToBeijing(value);
      },
    },
  })
  createDate: Date;

  @UpdateDateColumn({
    type: 'timestamp',
    transformer: {
      to(value: Date): number {
        return value ? value.getTime() : null;
      },
      from(value: number) {
        // 将UTC时间转换为北京时间
        return utcToBeijing(value);
      },
    },
  })
  updateDate: Date;

  @Column({
    comment: '作者id',
  })
  userId: number;

  @Column({
    comment: '作者名称',
  })
  authorName: string;

  @Column({
    comment: '说说内容',
    length: 200,
  })
 content: string;

  @Column({
    comment: '说说配图',
    nullable: true,
    length: 2000
  })
  images: string;

  @Column({
    comment: '头像'
  })
  avatar:string

  @Column({
    type: 'int',
    unsigned: true,
    comment: '点赞数',
    default: 0
  })
  like: UnsignedInt

  @Column({
    comment: 'ip',
    nullable: true
  })
  ip: string

  @Column({
    comment: '是否点赞',
    default: 0
  })
  action: 0|1

  @Column({
    type: 'int',
    unsigned: true,
    comment: '评论数',
    default: 0
  })
  comment_times: UnsignedInt

  @ManyToOne(() =>User, user => user.dailys)
  user: User[]

  @OneToMany(()=>Like, like=>like.daily)
  likes: Like[]

  @OneToMany(()=>Comment, comment=> comment.daily)
  comments: Comment[]
}

